package com.lionxing.DNTG;

import java.util.HashMap;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.menus.CCMenuItemSprite;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

import android.view.MotionEvent;

public class bookLayer extends CCLayer{

	
	int bookpagenum ;
	CCSprite spPage = null;
	CCMenu meunnext = null;
	CCMenu meunnext2= null;
	CGPoint _firstPoint;
	
	public static CCScene scene(int pic) {
		CCScene scene = CCScene.node();
		bookLayer layer = new bookLayer(pic);
		scene.addChild(layer);
		
		return scene;
	}
	
	
	protected bookLayer()
	{
		
		
	}
	
	protected bookLayer(int pic)
	{
	
		CGSize size = CCDirector.sharedDirector().winSize();
		
		if(pic != 154)
		{
			CCSprite nextitemnormal = CCSprite.sprite("next4.png");
			CCSprite nextitemhold = CCSprite.sprite("next3.png");
			
			nextitemnormal.setRotation(180.0f);
			nextitemhold.setRotation(180.0f);
			
			CCMenuItemSprite itemnext = CCMenuItemSprite.item(nextitemnormal, nextitemhold,this,"gotoNextleve");
			
			meunnext=  CCMenu.menu(itemnext);
			
			//meunnext.setRotation(180.0f);
			meunnext.setPosition(size.width - 20, size.height/2);
			
			addChild(meunnext);
		}
		
		
		if(pic != 1)
		{
			
			CCSprite nextitemnormal2 = CCSprite.sprite("next.png");
			CCSprite nextitemhold2 = CCSprite.sprite("next2.png");
			
			nextitemnormal2.setRotation(180.0f);
			nextitemhold2.setRotation(180.0f);
			
			CCMenuItemSprite itemnext2 = CCMenuItemSprite.item(nextitemnormal2, nextitemhold2,this,"gotoPreviousleve");
			
			meunnext2=  CCMenu.menu(itemnext2);
			
			//meunnext.setRotation(180.0f);
			meunnext2.setPosition(20, size.height/2);
			
			addChild(meunnext2);
		}
		

		

		CCMenuItemImage menuitembacktoleve = CCMenuItemImage.item("passda.png", "passd.png",  this, "gotoMain");

		CCMenu menubackleve = CCMenu.menu(menuitembacktoleve);
		menubackleve.setPosition(size.width/2, 30);
		addChild(menubackleve);
		
		this.setIsTouchEnabled(true);
		
		if(pic < 1)
		{
			pic =1;
		}
		
		if(pic > 154)
		{
			
			pic = 154;
		}
		
		bookpagenum = pic;
		String filename = String.format("book%d.png", bookpagenum);
		
		spPage = CCSprite.sprite(filename);
		
		
		spPage.setPosition(CGPoint.make(size.width/2, size.height/2));
		
		addChild(spPage ,-1);
		
	}
	
	
	 public boolean ccTouchesBegan(MotionEvent event)
	 {
		 _firstPoint = CCDirector.sharedDirector()
        	.convertToGL(CGPoint.make(event.getX(), event.getY()));
		 
		 if(meunnext != null)
		 {
			 meunnext.setVisible(false);
		 }
		 
		 
		 if(meunnext2 != null)
		 {
			 meunnext2.setVisible(false);
		 }
		 
	    return true;
	 }

	 public boolean ccTouchesMoved(MotionEvent event)
	 {
		 
	    	CGPoint convertedLocation = CCDirector.sharedDirector()
        	.convertToGL(CGPoint.make(event.getX(), event.getY()));
	    	
	    	
	    	CGPoint tmp = 	getFrothpoint(_firstPoint,convertedLocation,spPage.getPosition());
	    	
	    	_firstPoint = convertedLocation;
	    	
	    	if(IscanMove(tmp))
	    	{
	    		spPage.setPosition(tmp);
	    	}
	    	
	 	
	   return true;
	 }

	 public boolean ccTouchesEnded(MotionEvent event)
	 {
		 if(meunnext != null)
		 {
			 meunnext.setVisible(true);
		 }
		 
		 
		 if(meunnext2 != null)
		 {
			 meunnext2.setVisible(true);
		 }

	   	return true;
	 }

	 public boolean ccTouchesCancelled(MotionEvent event)
	 {
	    	
	    return true;
	 }
	
	 
	 private boolean IscanMove(CGPoint point)
	 {
		 
		 
		float heigth = spPage.getTextureRect().size.height;
		
		float width = spPage.getTextureRect().size.width;
		
		//CGPoint centP = spPage.getPosition();
		CGSize size = CCDirector.sharedDirector().winSize();
		if(point.x - width/2 > size.width/2)
		{
			return false;
			
		}
		
		if(point.x + width/2 < size.width/2)
		{
			return false;
			
		}
		
		if(point.y - heigth/2 > size.height/2)
		{
			return false;
			
		}
		
		if(point.y + heigth/2 < size.height/2)
		{
			return false;
			
		}
		 
		 
		 
		 return true;
	 }
	 
	 
	 private CGPoint getFrothpoint(CGPoint frist,CGPoint second,CGPoint third)
	 {
//		 	|	
//		 2	| 1
//		 ---|-------
//		 	|
//		 3	| 4
//		 	|

		 float x3 = second.x -frist.x;
		 float y3 = second.y -frist.y;
		 
		 
		 float x4 = third.x +x3;
		 float y4 = third.y + y3;
		 
		 return CGPoint.make(x4, y4);
		 
	 }
	 
	 public void gotoNextleve(Object sender)
	 {
		 
		 playSound.sharedDirector().playEffectSound(R.raw.kick);
		 if(bookpagenum < 0 || bookpagenum > 154)
		 {
			 
			 return;
		 }
		 
		 HashMap<String, Object> tmphash=  filemanager.sharedDirector().getHashmapgameinfo();
		 
		
	    	
		 bookpagenum = bookpagenum+1;
		 
		 tmphash.put("bookinfo", bookpagenum);
		 filemanager.sharedDirector().savegameinfo();
 		CCScene scene = bookLayer.scene(bookpagenum);
 		
  		 //CCDirector.sharedDirector().replaceScene(scene);
 		 scenemanager.go(scene);
		 
	 }
	 
	 public void gotoPreviousleve(Object sender)
	 {
		 
		 playSound.sharedDirector().playEffectSound(R.raw.kick);
		 if(bookpagenum < 0 || bookpagenum > 154)
		 {
			
		 }
	    	
		 bookpagenum = bookpagenum-1;
		 HashMap<String, Object> tmphash=  filemanager.sharedDirector().getHashmapgameinfo();
		 tmphash.put("bookinfo", bookpagenum);
		 filemanager.sharedDirector().savegameinfo();
 		CCScene scene = bookLayer.scene(bookpagenum);
  		 //CCDirector.sharedDirector().replaceScene(scene);
 		 scenemanager.go(scene);
		 
	 }
	 
	 public void gotoMain(Object sender)
	 {
		 playSound.sharedDirector().playEffectSound(R.raw.kick);
		 
			
			CCScene scene = MainLayer.scene();
			// CCDirector.sharedDirector().replaceScene(scene);
			 scenemanager.go(scene);
		 
	 }
	
	 
	 public  void onExit()
		{		
				this.removeAllChildren(true);
				super.onExit();
				
				spPage = null;
				meunnext = null;
				meunnext2= null;
		}
}
